import {gcj02ToBd09} from './lonlat-convertor';

/**
 * 初始化地图
 * @param mapId 地图ID
 * @param center 中心点
 * @param zoom 缩放级别；默认：15，范围 3-19
 * @return {module.exports.Map}
 */
const initMap = function (mapId, center = [106.478088, 29.670178], zoom = 15) {
  const BMap = window.BMap;
  const map = new BMap.Map(mapId);
  map.enableScrollWheelZoom(); // 启用滚轮放大缩小，默认禁用

  // 转换坐标点
  const lonlat = gcj02ToBd09(center[0], center[1]);

  // 设置中心点和缩放级别
  map.centerAndZoom(new BMap.Point(lonlat.lon, lonlat.lat), zoom);

  const topLeftControl = new BMap.ScaleControl({anchor: window.BMAP_ANCHOR_TOP_LEFT}); // 左上角，添加比例尺
  map.addControl(topLeftControl);

  const topLeftNavigation = new BMap.NavigationControl(); // 左上角，添加默认缩放平移控件
  map.addControl(topLeftNavigation);

  const topRightNavigation = new BMap.NavigationControl({
    anchor: window.BMAP_ANCHOR_TOP_RIGHT,
    type: window.BMAP_NAVIGATION_CONTROL_SMALL
  });
  // map.addControl(topRightNavigation); // 右上角，仅包含平移和缩放按钮

  // 添加地图类型控件
  map.addControl(new BMap.MapTypeControl({
    mapTypes: [
      window.BMAP_NORMAL_MAP,
      window.BMAP_HYBRID_MAP
    ]
  }));

  return map;
};

export {
  initMap
};

export default {
  initMap
};
